import Vue from 'vue'
import VueRouter from 'vue-router'
import Meta from 'vue-meta'

import Layout from '@/components/core/layout'

Vue.use(VueRouter)
Vue.use(Meta)

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/',
    name: 'dashboard',
    meta: {
      title: 'dashboard',
      icon: 'mdi-home',
    },
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home'),
        name: 'index',
        meta: {
          title: 'dashboard',
          keywords: 'wwww',
          description: 'ddddd',
          icon: 'home',
          // noCache: false
          // affix: true
        }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    meta: {
      title: 'login'
    },
    hidden: true
  },
]

import baseinfo from './modules/baseinfo'
import peers from './modules/peers'
import op from './modules/op'
import bill from './modules/bill'
import finance from './modules/finance'
import system from './modules/system'
import statistics from './modules/statistics'
import plan from './modules/plan'

export const asyncRoutes = [
  baseinfo,
  peers,
  plan,
  op,
  bill,
  finance,
  statistics,
  system,
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes: constantRoutes,
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    }
    if (to.hash) {
      return { selector: to.hash }
    }
    return { x: 0, y: 0 }
  }
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
